import { getCurrentInstance } from 'vue';
import { ElMessage } from 'element-plus';


const {proxy} = getCurrentInstance();


// 获取作者信息
export const fetchAuthorInfo = async () => {
  try {
    const response = await proxy.$http.get('/author/info');
    return response.data;
  } catch (error) {
    ElMessage.error('获取作者信息失败');
    throw error;
  }
};

// 获取作品列表
export const fetchBooks = async () => {
  try {
    const response = await proxy.$http.get('/author/books');
    return response.data.books;
  } catch (error) {
    ElMessage.error('获取作品列表失败');
    throw error;
  }
};

// 新建作品
export const createWork = async () => {
  try {
    const response = await proxy.$http.post('/author/books', { /* 作品数据 */ });
    return response.data;
  } catch (error) {
    ElMessage.error('新建作品失败');
    throw error;
  }
};

// 编辑章节信息
export const editChapter = async (bookId, chapter) => {
    try {
        const $http = useHttp();
        const response = await proxy.$http.put(`/books/${bookId}/chapters/${chapter.id}`, chapter);
        return response.data;
    } catch (error) {
        ElMessage.error('编辑章节失败');
        throw error;
    }
};
  
// 删除作品
export const deleteWork = async (bookId) => {
try {
    const response = await proxy.$http.delete(`/books/${bookId}`);
    return response.data;
} catch (error) {
    ElMessage.error('删除作品失败');
    throw error;
}
};

